//
//  UserAccount.swift
//  新浪微博01
//
//  Created by 李国伟 on 15/12/8.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit
//拼接路径
let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userInfo.plist")

class UserAccount: NSObject,NSCoding {

    var access_token: String?
    //给expireDate赋值，基本数据类型，不存在可选项的概念，带？kvc会出错
    var expires_in :NSTimeInterval = 0{
        didSet{
            expireDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    var uid :String?
    //过期时间
    var expireDate: NSDate?
    
    //大图标avatar_large
    var avatar_large: String?
    
    //昵称screen_name
    var screen_name: String?
    
    //使用kvc
    init(dic:[String : AnyObject]?) {
        super.init()
        setValuesForKeysWithDictionary(dic!)
    }
    
    //防止崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //描述，方便开着确认数据正确与否
    override var description: String{
        let keys = ["access_token","expires_in","uid","expireDate"]
        return dictionaryWithValuesForKeys(keys).description
    }
  
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "accesstoken")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatarlarge")
        aCoder.encodeObject(screen_name, forKey: "screenname")
        aCoder.encodeObject(expireDate, forKey: "expireDate")
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("accesstoken") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatarlarge") as? String
        screen_name = aDecoder.decodeObjectForKey("screenname") as? String
        expireDate = aDecoder.decodeObjectForKey("expireDate") as? NSDate
    }
}
